/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.pages.home;

import com.eelpo.cmmp.basic.dao.vo.SystemConfig;
import com.eelpo.cmmp.basic.services.ISystemConfigManageService;
import com.eelpo.cmmp.core.wicket.page.basic.DeniedPage;
import com.eelpo.cmmp.uac.constants.SystemConfigConstants;
import com.eelpo.cmmp.uac.session.UACSession;
import com.eelpo.common.util.DateFormatUtils;
import com.eelpo.common.util.StringUtils;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <pre>
 * 
 * 页面主框架，包含Top和Body两部分
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class Main extends DeniedPage {

	@SpringBean
	protected ISystemConfigManageService systemConfigManageService;

	private static final Logger logger = LoggerFactory.getLogger(Main.class);

	public Main() {

		// 检查用户密码是否过期
		checkPasswordPeriod();

		// 动态标题
		add(renderTitle());
	}

	public void checkPasswordPeriod() {

		// 获取密码有效期配置,默认为30天
		int period = 30;

		try {
			SystemConfig config = systemConfigManageService.getSystemConfigByKey(SystemConfigConstants.PASSWORD_PERIOD);
			if (StringUtils.isInteger(config.getConfigValue())) {
				period = Integer.parseInt(config.getConfigValue());
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}

		// 密码有效期到，强制修改密码
		if (DateFormatUtils.addDay(UACSession.get().getUser().getLastPasswordTime(), period).before(DateFormatUtils.getDate())) {

		}
	}

	public Label renderTitle() {
		IModel<String> titleModel = new Model<String>("电信群发营销管理系统");

		try {
			titleModel.setObject(systemConfigManageService.getSystemConfigByKey(SystemConfigConstants.SYSTEM_TITLE).getConfigValue());
		} catch (Exception e) {
			logger.error(e.getMessage());
		}

		return new Label("title", titleModel);
	}
}
